""" 
Copyright 2009 Texas A&M University
 
Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License. You may obtain a copy of the 
License at

http://www.apache.org/licenses/LICENSE-2.0
  
Unless required by applicable law or agreed to in writing, software distributed 
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License.
"""

from urllib import quote, urlencode
from logging import getLogger

from plone.memoize.view import memoize, memoize_contextless

from meercat.ui.plone.browser.content.base import BaseView



log = getLogger('meercat.ui.plone')


class ResourceView(BaseView):
    def __init__(self, *args, **kwargs):
        super(ResourceView, self).__init__(*args, **kwargs)
        
        self.request.set('disable_border',1)
        self._id = self.request.get('id',None)
        self._solrResponse = None
        self._resource = None
        if self._id:
            self._solrResponse = self.query(q='id:%s' % (self._id,),
                                            fl='*',
                                            qt='standard')
            if self._solrResponse and len(self._solrResponse.results) == 1:
                self._resource = self._solrResponse.results[0]
        
            
   
    @property
    @memoize
    def absolute_url(self):
        return self.request.URL
    
    @property
    @memoize
    def full_url(self):
        return "%s?%s" % (self.request.URL,urlencode(self.request.form))
    
    def hasResource(self):
        return bool(self._resource)
    
    def getResource(self):
        return self._resource